﻿using System;
using System.Collections.Generic;
using System.Data;

namespace SPDbFileExtractor.Framework.Entities
{
    /// <summary>
    /// Site
    /// </summary>
    public class Site
    {
        /// <summary>
        /// Gets or sets the site id.
        /// </summary>
        /// <value>
        /// The site id.
        /// </value>
        public Guid SiteId { get; set; }

        /// <summary>
        /// Gets or sets the full URL.
        /// </summary>
        /// <value>
        /// The full URL.
        /// </value>
        public string FullUrl { get; set; }

        /// <summary>
        /// Gets or sets the created datetime.
        /// </summary>
        /// <value>
        /// The created datetime.
        /// </value>
        public DateTime Created { get; set; }

        /// <summary>
        /// Gets or sets the name of the portal.
        /// </summary>
        /// <value>
        /// The name of the portal.
        /// </value>
        public string PortalName { get; set; }

        /// <summary>
        /// Gets or sets the portal URL.
        /// </summary>
        /// <value>
        /// The portal URL.
        /// </value>
        public string PortalUrl { get; set; }

        /// <summary>
        /// Gets or sets the host header.
        /// </summary>
        /// <value>
        /// The host header.
        /// </value>
        public string HostHeader { get; set; }

        /// <summary>
        /// Gets or sets the owner.
        /// </summary>
        /// <value>
        /// The owner.
        /// </value>
        public string Owner { get; set; }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return string.Format("{0}", SiteId);
        }

        /// <summary>
        /// Gets the collection from table.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static List<Site> GetCollectionFromTable(DataTable table)
        {
            if (table == null || table.Rows.Count == 0)
            {
                return new List<Site>();
            }

            var result = new List<Site>();

            foreach (DataRow row in table.Rows)
            {
                Site site = new Site();
                site.Created = (DateTime)row["Created"];
                site.FullUrl = row["FullUrl"] as string;
                site.HostHeader = row["HostHeader"] as string;
                site.Owner = row["Owner"] as string;
                site.PortalName = row["PortalName"] as string;
                site.PortalUrl = row["PortalUrl"] as string;
                site.SiteId = new Guid(row["SiteId"].ToString());

                result.Add(site);
            }

            return result;
        }
    }
}
